package com.goodboys;

import com.goodboys.base.filter.JwtFilter;
import com.goodboys.entity.UserBean;
import com.goodboys.utils.GoldMantisUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootApplication
@EnableScheduling
public class DemoApplication  extends SpringBootServletInitializer {

	/**
	 * 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(DemoApplication.class);
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	/*@Bean
	public JwtFilter jwtFilter(){
		return new JwtFilter();
	}
	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		registrationBean.setFilter(jwtFilter());
//		registrationBean.addUrlPatterns("/user");
//		registrationBean.addUrlPatterns("/info");
		registrationBean.setOrder(1);//多个filter执行的先后顺序
		return registrationBean;
	}*/

	@Bean
	public FilterRegistrationBean corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedOrigin("*");
		config.addAllowedHeader("*");
		config.addAllowedMethod("OPTIONS");
		config.addAllowedMethod("HEAD");
		config.addAllowedMethod("GET");
		config.addAllowedMethod("PUT");
		config.addAllowedMethod("POST");
		config.addAllowedMethod("DELETE");
		config.addAllowedMethod("PATCH");
		source.registerCorsConfiguration("/**", config);
		final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
		bean.setOrder(0);
		return bean;
	}

	@Bean
	public WebMvcConfigurer mvcConfigurer() {
		return new WebMvcConfigurerAdapter() {
			@Override
			public void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/**").allowedMethods("GET", "PUT", "POST", "GET", "OPTIONS");
			}
		};
	}

	//每分钟启动
	@Scheduled(cron = "0 0/60 8-22 * * ?")
	public void timerToNow(){
		System.out.println("start time ===========>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		List<UserBean> list = new ArrayList<>();
		list.add(new UserBean("N0047522", "chenjun18534@%"));
		list.add(new UserBean("N0043971","scz320901~"));
		list.stream().forEach(bean -> {
			GoldMantisUtil.readArticle(bean.getAccount());
			try {
				GoldMantisUtil.readMail(bean.getAccount(), bean.getPwd());
			} catch (IOException e) {
				e.printStackTrace();
			}
		});
		GoldMantisUtil.readArticle("N0046144");
		GoldMantisUtil.readArticle("N0039855");
		GoldMantisUtil.readArticle("N0046984");
		System.out.println("end time ===========>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}

	/**
	 * @description: 文件上传配置
	 */
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		//单个文件最大
		factory.setMaxFileSize("10MB");
		/// 设置总上传数据总大小
//		factory.setMaxRequestSize("50MB");
		return factory.createMultipartConfig();
	}
}
